var provinces
var citys
var districts
var send_util = require('../../utils/sendRequest.js')
var initProvince = function (that) {
  send_util.sendGet(
    'http://192.168.30.3:8080/gtxcx/region/getAllProvince.action',
    {},
    function (res) {
      that.setData({ provinces: res.data })
      initCity(that, res.data[0])
    },
    function (res) {
      console.log("get province fail:" + res.errMsg)
    }
  )
}
var initCity = function (that, provinceName) {
  send_util.sendGet(
    'http://192.168.30.3:8080/gtxcx/region/getCity.action',
    { provinceName: provinceName },
    function (res) {
      that.setData({ citys: res.data })
      initDistrict(that, res.data[0])
    },
    function (res) {
      console.log("get city fail:" + res.errMsg)
    }
  )
}
var initDistrict = function (that, cityName) {
  send_util.sendGet(
    'http://192.168.30.3:8080/gtxcx/region/getDistrict.action',
    { cityName: cityName },
    function (res) {
      that.setData({ districts: res.data })
    },
    function (res) {
      console.log("get districts fail:" + res.errMsg)
    }
  )
}
var initC = function (that, provinceName) {
  send_util.sendGet(
    'http://192.168.30.3:8080/gtxcx/region/getCity.action',
    { provinceName: provinceName },
    function (res) {
      that.setData({ citys: res.data })
    },
    function (res) {
      console.log("get city fail:" + res.errMsg)
    }
  )
}
var initD = function (that, cityName) {
  send_util.sendGet(
    'http://192.168.30.3:8080/gtxcx/region/getDistrict.action',
    { cityName: cityName },
    function (res) {
      that.setData({ districts: res.data })
    },
    function (res) {
      console.log("get districts fail:" + res.errMsg)
    }
  )
}
Page({
  data: {
    value: [1, 1, 1],
    provinces: [],
    citys: [],
    districts: [],
  },
  onLoad: function () {
    var that = this
    initProvince(that)
    initCity(that, '北京市')
    initDistrict(that, '北京市')
  },
  choose: function () {
    console.log("---------------")
    this.setData({ hidden: false })
  },
  bindChange: function (e) {
    var val = e.detail.value
    var that = this
    setTimeout(function () {
      initC(that, that.data.provinces[val[0]])
    }, 1000)
    setTimeout(function () {
      initD(that, that.data.citys[val[1]])
    }, 1000)
  }
})